package source;//hello

import java.util.*;
import java.awt.*;
import source.view.*;

/**
 *  The Driver class is the entry point into the application
 *  Main responsibilities include initializing the application,
 *  keeping track of main components, and beginning/ending turns.
 */

public class Driver {

	private static final int numPlayers = 2;
	private static final int startingOre = 100000;
	private static final int startingEnergy = 0;
	private static final int mapDimension = 10;
	
	private MainScreen mainScreen;
	private PlayerStatus players[];
	private int currentPlayer;
	private Map map;
	
	public Driver()
	{
		
		//create new controller object to attach to m_mainScreen
		Controller gameController = new Controller(this);
		
		map = new Map(TestMap.defaultMap());
		//map = new Map(mapDimension, mapDimension);
		
		//set up mainScreen
		mainScreen = new MainScreen(map, gameController);
		//Add controller to mainscreen.  Uncomment mouse listeners when needed.
		mainScreen.addKeyListener(gameController);
		//m_mainScreen.addMouseListener(gameController);
		//m_mainScreen.addMouseMotionListener(gameController);
		mainScreen.setFocusTraversalKeysEnabled(false);
		
		//Required to allow key events to be captured
		mainScreen.setFocusable(true);
		mainScreen.setVisible(true);
		
		//initialize players with 1 colonist and 2 explorers
		players = new PlayerStatus[numPlayers];
		
		// for pre-set map (DEMO)
		Tile[] tn = new Tile[2];
		tn[0] = map.getSquare(0, 0);
		tn[1] = map.getSquare(9, 9);
		
		for(int i = 0; i < numPlayers; i++)
		{
			//Tile t = map.getRandomStartingTile();
			Tile t = tn[i];		
			players[i] = new PlayerStatus(startingOre, startingEnergy, i, mapDimension);
			Colonist c = new Colonist(UnitType.COLONIST, players[i], t);
			Unit e1 = new Unit(UnitType.EXPLORER, players[i], t);
			Unit e2 = new Unit(UnitType.EXPLORER, players[i], t);
			players[i].addUnit(c);
			players[i].addUnit(e1);
			players[i].modifyExplored(e1);
			players[i].addUnit(e2);
			//players[i].modifyExplored(e2);
			t.addUnit(c);
			t.addUnit(e1);
			t.addUnit(e2);
			MainScreen.writeToConsole("Driver: Player " + i + " Starts at (" + t.getX() + ", " + t.getY() + ")", Color.RED);
		}
		Tile currentSelection = players[currentPlayer].getCurrentUnit().getPosition();

		
		currentPlayer = 0;
		mainScreen.getUnitOverview().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getStructureOverview().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getStatusViewport().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getAreaViewport().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getAreaViewport().setSelected(currentSelection.getX(), currentSelection.getY());
		
		mainScreen.getStatusViewport().update();
		mainScreen.focusMap(currentSelection.getX(), currentSelection.getY());
		mainScreen.update();
		players[currentPlayer].nextTurn();
	}
	
	public void nextTurn()
	{
		currentPlayer = (currentPlayer + 1)%numPlayers;
		//TODO implement PlayerStatus.nextTurn()
		players[currentPlayer].nextTurn();
		Vector<Unit> v = players[currentPlayer].getMissions();
		Vector<Unit> toBeRemoved = new Vector<Unit>();
		for(Unit u : v)
		{
			if(map.move(u, u.getDestinationTile()))
			{
				toBeRemoved.add(u);
			}
		}
		for(Unit u1 : toBeRemoved)
		{
			u1.setDestinationTile(null);
			u1.getPlayer().missionDone(u1);
		}
		mainScreen.getUnitOverview().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getStructureOverview().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getStatusViewport().setCurrentPlayer(players[currentPlayer]);
		mainScreen.getStatusViewport().displayUnit();
		mainScreen.getAreaViewport().setCurrentPlayer(players[currentPlayer]);
	}
	
	//TODO implement PlayerStatus class
	public PlayerStatus getCurrentPlayer()
	{
		return players[currentPlayer];
	}
	
	public MainScreen getMainScreen()
	{
		return mainScreen;
	}
	
	public Map getMap()
	{
		return map;
	}
	public static void main(String[] args) {
		Driver d = new Driver();
	}

}
